VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsArticulo"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Dim vSQL As String

Public pNumError As Long
Public pDesError As String
Public pCanReg As Long
Public pRS As ADODB.Recordset
Public pIdDevuelto As Long


Private vIdArticulo As Long


Public Property Let idArticulo(ByVal VData As Long)
    vIdArticulo = VData
End Property
Public Property Get idArticulo() As Long
    idArticulo = vIdArticulo
End Property

Private Sub AsignarValores(Optional ByVal rs As ADODB.Recordset, Optional pNoAsigRs As Boolean)
    If Not pNoAsigRs Then
        Set Me.pRS = rs
    End If
    Me.pCanReg = objCnxAeros.pCanReg
    Me.pDesError = objCnxAeros.pDesError
    Me.pIdDevuelto = objCnxAeros.pUltIdenOtor
    Me.pNumError = objCnxAeros.pNumError
End Sub

Public Function BajaLogicaArticuloFarmacia(ByVal pfechaBaja As Date) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_delete_logico_ARTICULOS", adCmdStoredProc, idArticulo, pfechaBaja) Then
       BajaLogicaArticuloFarmacia = True
    Else
       BajaLogicaArticuloFarmacia = False
    End If
    GrabarLOG Me.idArticulo, "ARTICULOS", "clsArticulo", OperacionEnPantalla.eBaja
    
    AsignarValores , True
End Function

Public Function BajaLogicaArticuloElaborado(ByVal pIdAlmacenLocal As Long, ByVal pfechaBaja As Date, ByVal pmotivoBaja As String) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_delete_ARTICULOS_X_ALMACEN", adCmdStoredProc, idArticulo, pIdAlmacenLocal, pfechaBaja, pmotivoBaja) Then
       BajaLogicaArticuloElaborado = True
    Else
       BajaLogicaArticuloElaborado = False
    End If
    GrabarLOG Me.idArticulo, "ARTICULOS_X_ALMACEN", "clsArticulo", OperacionEnPantalla.eBaja, "alm_idesec: " & IdAlmacenLocal
    
    AsignarValores , True

End Function

Public Function BajaFisicaProveedoresDeArticulo() As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_delete_PROVEEDORES_DE_UN_ARTICULO", adCmdStoredProc, idArticulo) Then
       BajaFisicaProveedoresDeArticulo = True
    Else
       BajaFisicaProveedoresDeArticulo = False
    End If
    GrabarLOG Me.idArticulo, "ARTICULOS", "clsArticulo", OperacionEnPantalla.eBaja
    
    AsignarValores , True

End Function


Public Function AltaArticuloFarmacia(ByVal pudm_idesec As Long, ByVal prda_idesec As Long, ByVal part_nom As String, _
                                ByVal part_stomin As String, ByVal part_codbar As String, ByVal part_val As Double, _
                                ByVal part_valcos As Double, ByVal part_iva As Double, ByVal pmar_idesec As Long, _
                                ByVal pmod_idesec As Long, ByVal ptan_idesec As Long, ByVal ppri_idesec As Long, _
                                ByVal pumo_idesec As Long, ByVal part_maxdiv As Long, ByVal part_marvalfij As Double, _
                                ByVal part_marpor As Double, ByVal part_cuotas As Long) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_insert_ARTICULOS", adCmdStoredProc, pudm_idesec, prda_idesec, part_nom, part_stomin, part_codbar, _
                                                                        part_val, part_valcos, part_iva, pmar_idesec, pmod_idesec, _
                                                                        ptan_idesec, ppri_idesec, pumo_idesec, IIf(part_maxdiv = 0, Null, part_maxdiv), _
                                                                        IIf(part_marvalfij = 0, Null, part_marvalfij), IIf(part_marpor = 0, Null, part_marpor), _
                                                                        IIf(part_cuotas = 0, Null, part_cuotas)) Then
        AltaArticuloFarmacia = True
        Me.idArticulo = objCnxAeros.pUltIdenOtor
        GrabarLOG Me.idArticulo, "ARTICULOS", "clsArticulo", OperacionEnPantalla.eAlta
    Else
        AltaArticuloFarmacia = False
    End If
    AsignarValores , True

End Function

Public Function AltaProveedorDeArticulo(ByVal pProveedor As Long) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_insert_PROVEEDORES_X_ARTICULOS", adCmdStoredProc, Me.idArticulo, pProveedor) Then
        AltaProveedorDeArticulo = True
        GrabarLOG Me.idArticulo, "PROVEEDORES_X_ARTICULOS", "clsArticulo", OperacionEnPantalla.eModificacion
    Else
        AltaProveedorDeArticulo = False
    End If
    AsignarValores , True

End Function


Public Function AltaArticuloAlmacen(ByVal pIdAlmacenLocal As Long, ByVal pValorFijo As Double, ByVal pValorPorcentaje As Double, ByVal pStockMinimo As Double) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_insert_ARTICULOS_X_ALMACEN", adCmdStoredProc, Me.idArticulo, pIdAlmacenLocal, IIf(pValorFijo = 0, Null, pValorFijo), IIf(pValorPorcentaje = 0, Null, pValorPorcentaje), pStockMinimo) Then
        AltaArticuloAlmacen = True
        GrabarLOG Me.idArticulo, "ARTICULOS_X_ALMACEN", "clsArticulo", OperacionEnPantalla.eAlta, "alm_idesec: " & pIdAlmacenLocal
    Else
        AltaArticuloAlmacen = False
    End If
    AsignarValores , True

End Function



Public Function ModificacionArticulo(ByVal pUnidadMedida As Integer, ByVal pRubroArticulo As Integer, ByVal pnombre As String, ByVal pStockMinimo As Double, ByVal pCodigoBarra As String, ByVal pValor As Double, ByVal pValorCos As Double, ByVal pArticuloIVA As Double, ByVal pMarca As Integer, ByVal pModelo As Integer, ByVal pPresentacion As Integer, ByVal pPrioridad As Integer, ByVal pUnidadMonetaria As Integer, ByVal pArticuloMaxDiv As Integer, ByVal pValorFijo As Double, ByVal pValorPorcentaje As Double, ByVal pCuotas As Integer) As Boolean

    If objCnxAeros.EjeProcedimiento("jsp_update_ARTICULOS", adCmdStoredProc, Me.idArticulo, pUnidadMedida, pRubroArticulo, pnombre, pStockMinimo, pCodigoBarra, pValor, pValorCos, pArticuloIVA, pMarca, pModelo, pPresentacion, pPrioridad, pUnidadMonetaria, pArticuloMaxDiv, IIf(pValorFijo = 0, Null, pValorFijo), IIf(pValorPorcentaje = 0, Null, pValorPorcentaje), IIf(pCuotas = 0, Null, pCuotas)) Then
        ModificacionArticulo = True
        GrabarLOG Me.idArticulo, "ARTICULOS", "clsArticulo", OperacionEnPantalla.eModificacion
    Else
        ModificacionArticulo = False
    End If
    AsignarValores , True

End Function
                    

Public Function ModificacionArticuloElaborado(ByVal pUnidadMedida As Integer, ByVal pRubroArticulo As Integer, ByVal pnombre As String, ByVal pCodigoBarra As String, ByVal pArticuloIVA As Double, ByVal pMarca As Integer, ByVal pModelo As Integer, ByVal pPresentacion As Integer, ByVal pPrioridad As Integer, ByVal pUnidadMonetaria As Integer, ByVal pArticuloMaxDiv As Integer, ByVal pCuotas As Integer) As Boolean
    
    If objCnxAeros.EjeProcedimiento("jsp_update_ARTICULOS_ELABORADOS", adCmdStoredProc, Me.idArticulo, pUnidadMedida, pRubroArticulo, pnombre, pCodigoBarra, pArticuloIVA, pMarca, pModelo, pPresentacion, pPrioridad, pUnidadMonetaria, pArticuloMaxDiv, pCuotas) Then
        
        ModificacionArticuloElaborado = True
    Else
        ModificacionArticuloElaborado = False
    End If
End Function


Public Function ModificacionArticuloAlmacen(ByVal pIdAlmacenLocal As Long, ByVal pValorFijo As Double, ByVal pValorPorcentaje As Double, ByVal pValorCos As Double, ByVal pStockMinimo As Double) As Boolean
            
    If objCnxAeros.EjeProcedimiento("jsp_update_ARTICULOS_X_ALMACEN", adCmdStoredProc, Me.idArticulo, pIdAlmacenLocal, pValorFijo, pValorPorcentaje, pValorCos, pStockMinimo) Then
        ModificacionArticuloAlmacen = True
        GrabarLOG Me.idArticulo, "ARTICULOS_X_ALMACEN", "clsArticulo", OperacionEnPantalla.eModificacion, "alm_idesec: " & pIdAlmacenLocal
    Else
        ModificacionArticuloAlmacen = False
    End If

End Function

'###################################################################################

Public Function sel_Marcas()
Dim rs As New ADODB.Recordset
Dim strSQL As String
    
    strSQL = "SELECT id_marca, marca FROM MARCAS"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs
    
End Function

Public Function sel_Categorias()
Dim rs As New ADODB.Recordset
Dim strSQL As String

    strSQL = "SELECT id_categoria, categoria FROM CATEGORIAS"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Function

Public Function sel_TiposUsos()
Dim rs As New ADODB.Recordset
Dim strSQL As String

    strSQL = "SELECT id_tipo_uso, tipo_uso FROM TIPOS_USO"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Function

Public Function sel_Lineas()
Dim rs As New ADODB.Recordset
Dim strSQL As String

    strSQL = "SELECT id_linea, linea FROM LINEAS"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Function

Public Sub sel_Articulos()
Dim rs As New ADODB.Recordset
Dim strSQL As String

    strSQL = " SELECT a.id_articulo as [Id Articulo], l.linea + ' ' + a.articulo as Articulo " & _
             " FROM Articulos a, lineas l " & _
             " WHERE a.fecha_baja IS NULL AND a.id_linea = l.id_linea " & _
             " ORDER BY l.orden, l.linea, a.articulo"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub

Public Function ins_Articulo(ByVal particulo As String, ByVal pid_linea As Long, _
                                ByVal pid_marca As Long, ByVal pid_categoria As Long, ByVal pprecio As Double, ByVal pprecio_venta As Double, _
                                ByVal ppto_pedido As Long, ByVal pid_tipo_uso As Long) As Boolean
Dim strSQL As String

    strSQL = "INSERT INTO ARTICULOS " & _
                "(articulo,id_linea,id_marca,id_categoria,precio,precio_venta,pto_pedido,id_tipo_uso) " & _
                "values ('" & Left(particulo, 50) & "', " & IIf(pid_linea = 0, "Null", pid_linea) & ", " & _
                IIf(pid_marca = 0, "Null", pid_marca) & ", " & IIf(pid_categoria = 0, "Null", pid_categoria) & ", " & pprecio & "," & pprecio_venta & ", " & _
                ppto_pedido & ", " & IIf(pid_tipo_uso = 0, "Null", pid_tipo_uso) & ")"
    If objCnxAeros.EjecutarComando(strSQL) Then
        ins_Articulo = True
    Else
        ins_Articulo = False
    End If
    AsignarValores , True

End Function

Public Function upd_Articulo(ByVal pidArticulo As Long, ByVal particulo As String, ByVal pid_linea As Long, _
                                ByVal pid_marca As Long, ByVal pid_categoria As Long, ByVal pprecio As Double, ByVal pprecio_venta As Double, _
                                ByVal ppto_pedido As Long, ByVal pid_tipo_uso As Long) As Boolean
Dim strSQL As String

    strSQL = "UPDATE ARTICULOS SET " & _
                "articulo = '" & Left(particulo, 50) & "', id_linea = " & _
                IIf(pid_linea = 0, "Null", pid_linea) & ", id_marca = " & IIf(pid_marca = 0, "Null", pid_marca) & ",id_categoria = " & _
                IIf(pid_categoria = 0, "Null", pid_categoria) & ", precio = " & pprecio & ", precio_venta = " & pprecio_venta & ", pto_pedido = " & ppto_pedido & _
                ", id_tipo_uso = " & IIf(pid_tipo_uso = 0, "Null", pid_tipo_uso) & _
                " WHERE id_articulo = " & pidArticulo
    If objCnxAeros.EjecutarComando(strSQL) Then
        upd_Articulo = True
    Else
        upd_Articulo = False
    End If

End Function

Public Function ins_Articulos_X_Proveedor(ByVal pid_articulo As Long, ByVal pid_proveedor As Long) As Boolean
Dim strSQL As String

    strSQL = "INSERT INTO ARTICULOS_X_PROVEEDOR (id_articulo, id_proveedor) " & _
                "values (" & pid_articulo & ", " & pid_proveedor & ")"
    If objCnxAeros.EjecutarComando(strSQL) Then
        ins_Articulos_X_Proveedor = True
    Else
        ins_Articulos_X_Proveedor = False
    End If
    AsignarValores , True

End Function

Public Sub sel_Articulo(ByVal pid_articulo)
Dim rs As New ADODB.Recordset
Dim strSQL As String

    strSQL = "SELECT articulo,codigo_barras,id_linea,id_marca,id_categoria,precio,precio_venta,pto_pedido, " & _
                "tamanio,id_tipo_uso, cantidad " & _
                "FROM ARTICULOS " & _
                "WHERE fecha_baja IS NULL AND id_articulo = " & pid_articulo
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub

Public Function del_Articulo(ByVal pid_articulo As Long, ByVal pfechaBaja As Date, ByVal pmotivoBaja As String) As Boolean
Dim strSQL As String

    strSQL = "UPDATE ARTICULOS SET " & _
                "fecha_baja = '" & pfechaBaja & "', motivo_baja = '" & pmotivoBaja & "' " & _
                "WHERE id_articulo = " & pid_articulo
    If objCnxAeros.EjecutarComando(strSQL) Then
        del_Articulo = True
    Else
        del_Articulo = False
    End If

End Function


Public Function upd_cantidad(ByVal id_articulo As Long, _
                             ByVal Cantidad As Long, _
                             ByVal tipo_movimiento As Boolean) As Boolean
    
Dim strSQL As String
    
    If tipo_movimiento = False Then
        Cantidad = Cantidad * -1
    End If
    
    strSQL = " UPDATE articulos SET Cantidad = Cantidad + " & Cantidad & _
           " WHERE id_articulo = " & id_articulo
        
    If objCnxAeros.EjecutarComando(strSQL) Then
        upd_cantidad = True
    Else
        upd_cantidad = False
    End If

End Function

Public Function upd_ptoPedido(ByVal id_articulo As Long, ByVal Cantidad As Long) As Boolean
Dim strSQL As String
    
    strSQL = " UPDATE articulos SET pto_pedido = " & Cantidad & _
           " WHERE id_articulo = " & id_articulo
        
    If objCnxAeros.EjecutarComando(strSQL) Then
        upd_ptoPedido = True
    Else
        upd_ptoPedido = False
    End If

End Function

Public Sub sel_StockTotal()
Dim strSQL As String
Dim rs As New ADODB.Recordset

    strSQL = " SELECT a.id_articulo as [Id Articulo], " & _
                " (l.linea & ' ' & a.articulo) AS Articulo, " & _
                " a.pto_pedido as [Pto Pedido]," & _
                " a.cantidad as Cantidad, " & _
                " a.cantidad - a.pto_pedido as [Diferencia],  " & _
                " 0 as [Pedido] " & _
                "FROM ARTICULOS a, LINEAS l " & _
                "WHERE a.id_linea = l.id_linea AND a.fecha_baja is null " & _
                "ORDER BY l.orden, l.linea, a.articulo "
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub

'Optiene el top 10 de los articulos mas vendidos en el mes actual,
'es solo de muestra, nose bien que reporte hacer se podria generar
'una modificacion seria que lea el mes que quiere consultar etc.
Public Sub sel_articulostop10()
Dim strSQL As String
Dim rs As New ADODB.Recordset

    strSQL = " SELECT  TOP 10 DatePart(""m"",movimientos.fecha) AS Mes, " & Chr(10) _
            & "    articulos.id_articulo, " & Chr(10) _
            & "    lineas.linea + ' ' + articulos.articulo AS Articulo, " & Chr(10) _
            & "    sum(movimientos.cantidad) AS Cantidad " & Chr(10) _
            & " FROM lineas " & Chr(10) _
            & " INNER JOIN (articulos INNER JOIN movimientos ON articulos.id_articulo=movimientos.id_articulo)  " & Chr(10) _
            & "   ON lineas.id_linea=articulos.id_linea " & Chr(10) _
            & " WHERE movimientos.tipo_mov=False  " & Chr(10) _
            & " AND DatePart(""m"",movimientos.fecha)=DatePart(""m"",now()) " & Chr(10) _
            & " AND DatePart(""y"",movimientos.fecha)=DatePart(""y"",now()) " & Chr(10) _
            & " GROUP BY DatePart(""m"",movimientos.fecha),  " & Chr(10) _
            & "  articulos.id_articulo,  " & Chr(10) _
            & "  lineas.linea + ' ' + articulos.articulo;"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub

Public Function del_ProveedoresDeUnArticulo(ByVal pid_articulo As Long) As Boolean
Dim strSQL As String

    strSQL = "DELETE FROM ARTICULOS_X_PROVEEDOR WHERE id_articulo = " & pid_articulo
    If objCnxAeros.EjecutarComando(strSQL) Then
        del_ProveedoresDeUnArticulo = True
    Else
        del_ProveedoresDeUnArticulo = False
    End If

End Function

Public Sub sel_ProveedoresDeUnArticulo(ByVal pid_articulo As Long)
Dim strSQL As String
Dim rs As New ADODB.Recordset

    strSQL = "SELECT ap.id_proveedor, ap.id_articulo   " & Chr(10) _
            & "FROM ARTICULOS_X_PROVEEDOR ap " & Chr(10) _
            & "INNER JOIN ARTICULOS a ON ap.id_articulo = a.id_articulo  " & Chr(10) _
            & "WHERE a.fecha_baja IS NULL AND ap.id_articulo = " & pid_articulo
          
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub

Public Sub sel_ArticulosdeUnProveedor(ByVal pidProveedor As Long)
Dim strSQL As String
Dim rs As New ADODB.Recordset

    strSQL = " SELECT a.id_articulo, l.linea + ' ' + a.articulo as Articulo, a.pto_pedido " & _
             "FROM Articulos a, lineas l, ARTICULOS_X_PROVEEDOR ap " & _
             "WHERE a.fecha_baja IS NULL AND a.id_articulo = ap.id_articulo AND " & _
             "a.id_linea = l.id_linea AND " & _
             "ap.id_proveedor = " & pidProveedor & _
             " ORDER BY l.orden, l.linea, a.articulo"
    Set rs = objCnxAeros.EjecutarConsulta(strSQL)
    AsignarValores rs

End Sub
